package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.DiseaseAddDto;
import com.njworkorder.Entity.Disease;
import com.njworkorder.Service.DiseaseService;
import com.njworkorder.Utils.NoCode;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "病害管理", description = "病害管理相关接口")
@RestController
@RequestMapping("/disease")
public class DiseaseController {

    private final DiseaseService diseaseService;
    public DiseaseController(DiseaseService diseaseService) {
        this.diseaseService = diseaseService;
    }

    @Operation(summary = "添加病害信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody DiseaseAddDto diseaseAddDto,@RequestHeader("Create-Ap-Id") String createApId){

        Disease disease = new Disease();
        BeanUtils.copyProperties(diseaseAddDto,disease);
        disease.setCreateApId(createApId);

        boolean save = diseaseService.save(disease);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");

    }

    @Operation(summary = "根据id修改病害信息")
    @PostMapping("/updateInfoById/{id}")
    public Result<String> updateInfoById(@RequestBody DiseaseAddDto diseaseAddDto,@PathVariable("id") String id){

        Disease disease = new Disease();
        BeanUtils.copyProperties(diseaseAddDto,disease);
        disease.setId(id);

        boolean b = diseaseService.updateById(disease);
        if (b){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }

    @Operation(summary = "根据id删除病害信息")
    @GetMapping("/deleteInfoById")
    public Result<String> deleteInfoById(@RequestParam("id") String id){
        boolean b = diseaseService.removeById(id);
        if (b){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @Operation(summary = "根据id查询病害信息")
    @GetMapping("/getInfoById")
    public Result<Disease> getInfoById(@RequestParam("id") String id){
        Disease disease = diseaseService.getById(id);
        return ResultUtil.success(disease);
    }

    @Operation(summary = "查询病害信息列表")
    @GetMapping("/getList")
    public Result<Page<Disease>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize,
                                         @RequestParam(value = "diseaseModule",required = false) String diseaseModule){


        List<Map<String, String>> dModel = NoCode.getDModel();

        String diseaseCategory = "";
        if(StringUtils.isNotEmpty(diseaseModule)){
            List<Map<String, String>> dModel2 = dModel.stream().filter(f -> f.get("dModel").equals(diseaseModule)).toList();
            if (!dModel2.isEmpty()){
                diseaseCategory = dModel2.getFirst().get("name");
            }
        }

        Page<Disease> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<Disease> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(diseaseCategory),Disease::getDiseaseCategory,diseaseCategory);
        queryWrapper.apply("1=1");
        queryWrapper.orderByDesc(Disease::getPublishTime);
        Page<Disease> diseasePage = diseaseService.page(page, queryWrapper);
        return ResultUtil.success(diseasePage);
    }


    @Operation(summary = "查询病害结构种类")
    @GetMapping("/getBridgeDiseaseStructure")
    public Result<List<Map<String, Object>>> getBridgeDiseaseStructure(){
        LambdaQueryWrapper<Disease> wrapper = new LambdaQueryWrapper<Disease>()
                .select(Disease::getBridgeDiseaseStructure)
                .ne(Disease::getBridgeDiseaseStructure,"").apply("1=1")
                .groupBy(Disease::getBridgeDiseaseStructure);
        List<Map<String, Object>> maps = diseaseService.listMaps(wrapper);
        return ResultUtil.success(maps);
    }

    @Operation(summary = "查询病害类别", description = "可根据病害结构查询病害类别")
    @GetMapping("/getBridgeDiseaseCategory")
    public Result<List<Map<String, Object>>> getBridgeDiseaseCategory(@RequestParam(value = "BridgeDiseaseStructure",required = false) String BridgeDiseaseStructure){
        LambdaQueryWrapper<Disease> wrapper = new LambdaQueryWrapper<Disease>()
                .select(Disease::getBridgeDiseaseCategory)
                .ne(Disease::getBridgeDiseaseCategory,"").apply("1=1")
                .eq(BridgeDiseaseStructure != null && !BridgeDiseaseStructure.isEmpty(),Disease::getBridgeDiseaseStructure,BridgeDiseaseStructure)
                .groupBy(Disease::getBridgeDiseaseCategory);
        List<Map<String, Object>> maps = diseaseService.listMaps(wrapper);
        return ResultUtil.success(maps);
    }

}
